<?php

header('Content-type: text/xml');
require_once 'dbCon.php';

$categoryQuery = 'SELECT * FROM ak_categories ORDER BY ctg_position ASC';
$categories = mysql_query($categoryQuery) or die('Data not found for categoryQuery.');

$xml_output = "<?xml version='1.0' encoding='UTF-8'?>\n";
$xml_output .= "<navi>\n";

while ($rowCtg = mysql_fetch_assoc($categories)) {
    $xml_output .= "\t<hauptnavi name='".$rowCtg['ctg_label']."' startX='".$rowCtg['ctg_start_x']."' startY='".$rowCtg['ctg_start_y']."'>\n";
    $xml_output .= "\t\t<survey>\n";
    
    $buttonsQuery = 'SELECT * FROM ak_buttons WHERE btn_category_id = '.$rowCtg['ctg_id'].' AND btn_project_id = 0';
	$buttons = mysql_query($buttonsQuery) or die('Data not found for buttonsQuery 1 on category: '.$rowCtg['ctg_alias'] );
	while ($rowBtn = mysql_fetch_assoc($buttons)) {
		$xml_output .= "\t\t\t<image title='".$rowBtn['btn_label']."' thumb='".$rowBtn['btn_thumb']."' file='".$rowBtn['btn_img']."' btnX='".$rowBtn['btn_x']."' btnY='".$rowBtn['btn_y']."' imgX='".$rowBtn['btn_content_x']."' imgY='".$rowBtn['btn_content_y']."' imgWidth='".$rowBtn['btn_content_width']."' imgHeight='".$rowBtn['btn_content_height']."'></image>\n";
	}
    $xml_output .= "\t\t</survey>\n";
    $xml_output .= "\t\t<subnavi>\n";
    
    $projectsQuery = 'SELECT * FROM ak_projects WHERE prj_category_id = '.$rowCtg['ctg_id'].' ORDER BY prj_position ASC';
	$projects = mysql_query($projectsQuery) or die("Data not found for projectsQuery.");
    while ($rowPrj = mysql_fetch_assoc($projects)) {	
    	$xml_output .= "\t\t\t<thumb file='".$rowPrj['prj_thumb']."' name='".$rowPrj['prj_label']."'>\n";
    	$xml_output .= "\t\t\t\t<content>\n";
    	
    	$buttonsQuery = 'SELECT * FROM ak_buttons WHERE btn_project_id = '.$rowPrj['prj_id'];
		$buttons = mysql_query($buttonsQuery) or die('Data not found for buttonsQuery 2 on project: '.$rowPrj['prj_alias']);
		while ($rowBtn = mysql_fetch_assoc($buttons)) {
    		$xml_output .= "\t\t\t\t\t<image title='".$rowBtn['btn_label']."' thumb='".$rowBtn['btn_thumb']."' file='".$rowBtn['btn_img']."' btnX='".$rowBtn['btn_x']."' btnY='".$rowBtn['btn_y']."' imgX='".$rowBtn['btn_content_x']."' imgY='".$rowBtn['btn_content_y']."' imgWidth='".$rowBtn['btn_content_width']."' imgHeight='".$rowBtn['btn_content_height']."'>\n";
    		
    		// Escaping illegal characters
	        $rowBtn['btn_text'] = str_replace("&", "&amp;", $rowBtn["btn_text"]);
	        $rowBtn["btn_text"] = str_replace("<", "&lt;", $rowBtn["btn_text"]);
	        $rowBtn["btn_text"] = str_replace(">", "&gt;", $rowBtn["btn_text"]);
	        $rowBtn["btn_text"] = str_replace("'", "&apos;", $rowBtn["btn_text"]);
	        $rowBtn["btn_text"] = str_replace("\"", "&quot;", $rowBtn["btn_text"]);
    		
    		$xml_output .= "\t\t\t\t\t\t<![CDATA[".$rowBtn["btn_text"]."]]>\n";
    		$xml_output .= "\t\t\t\t\t</image>\n";
		}
		$xml_output .= "\t\t\t\t</content>\n";
		$xml_output .= "\t\t\t</thumb>\n";
    }
    $xml_output .= "\t\t</subnavi>\n";
    $xml_output .= "\t</hauptnavi>\n";
}
$xml_output .= "</navi>\n";

mysql_close($conn);

echo $xml_output;

?> 